package com.atguigu.userprofile.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.userprofile.bean.TagCondition;
import com.atguigu.userprofile.bean.TaskInfo;
import com.atguigu.userprofile.bean.UserGroup;
import com.atguigu.userprofile.service.UserGroupService;
import com.atguigu.userprofile.utils.RedisUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangchen
 * @since 2021-05-04
 */
@RestController
public class UserGroupController {

    @Autowired
    UserGroupService userGroupService;

    @RequestMapping("/user-group-list")
    @CrossOrigin
    public String  getUserGroupList(@RequestParam("pageNo")int pageNo , @RequestParam("pageSize") int pageSize){
        int startNo=(  pageNo-1)* pageSize;
        int endNo=startNo+pageSize;

        QueryWrapper<UserGroup> queryWrapper = new QueryWrapper<>();
        int count = userGroupService.count(queryWrapper);

        queryWrapper.orderByDesc("id").last(" limit " + startNo + "," + endNo);
        List<UserGroup> userGroupList = userGroupService.list(queryWrapper);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("detail",userGroupList);
        jsonObject.put("total",count);

        return  jsonObject.toJSONString();
    }


    @PostMapping("/user-group")
    public String createUserGroup(@RequestBody UserGroup userGroup){
        //  根据json制作实体bean 遇到[] 做list 遇到{} 使用实体bean或者hashmap
        //1  分群基本信息保存在mysql中  生成主键
        userGroupService.saveUserGroupInfo(userGroup);
        //2  通过clickhouse中已经按照标签存储的bitmap集合 ，进行交、并集计算，获得分群的bitmap，再写入clickhouse中。
        //
        //生成人群包
        userGroupService.genUserGroup(userGroup);

        //3 对应下游的高频的访问，clickhouse并不适合，redis更适合。
        //  要把clickhouse中bitmap的结果拷贝一份放到redis中。
        Long userGroupNum = userGroupService.saveUserGroupRedis(userGroup);

        //4 补充分群人数
        userGroup.setUserGroupNum(userGroupNum);
        userGroupService.saveUserGroupInfo(userGroup);

        return "success";
    }


    @PostMapping("/user-group-evaluate")
    public Long   evaluateNum(@RequestBody UserGroup userGroup){

        Long num = userGroupService.cardinalityUserGroup(userGroup);
        return num;
    }

    @PostMapping("/user-group-refresh/{id}")
    public String refreshUserGroup(@PathVariable("id") String userGroupId,@RequestParam("busiDate") String busiDate){
        //1  从mysql中把分群基本信息提取处理 根据userGroupId
        UserGroup userGroup = userGroupService.getById(userGroupId);
        //2 更新分群包
        List<TagCondition> tagConditions = JSON.parseArray(userGroup.getConditionJsonStr(), TagCondition.class);
        userGroup.setTagConditions(tagConditions);
        userGroup.setBusiDate(busiDate);
        userGroupService.genUserGroup(userGroup);
        //3 更新redis
        Long userGroupNum = userGroupService.saveUserGroupRedis(userGroup);

        // 更新人数  mysql
        userGroup.setUserGroupNum(userGroupNum);
        userGroup.setUpdateTime(new Date());
        userGroupService.saveUserGroupInfo(userGroup);

        return "success";
    }
}

